﻿<div id="gridDictionaries"></div>
<div id="dlgAddDictionary" style="display:none;padding-left:60px;">
    <form id="formAddDictionary"></form>
</div>
<div id="dlgUpdateDictionary" style="display:none;padding-left:60px;">
    <form id="formUpdateDictionary"></form>
</div>

<script type="text/javascript">
    var gridDictionaries = null;
    var dlgAddDictionary = null;
    var dlgUpdateDictionary = null;
    var formAddDictionary = null;
    var formUpdateDictionary = null;
</script>

<script type="text/javascript">
    function addDictionary() {
        $.post('/API/Dictionary/Create', formAddDictionary.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('添加成功', '提示');
                loadDictionaries();
                dlgAddDictionary.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function showDlgAddDictionary() {
        if (dlgAddDictionary) {
            dlgAddDictionary.show();
        }
        else {
            dlgAddDictionary = $.ligerDialog.open({
                target: $('#dlgAddDictionary'),
                width: 440,
                height: 280,
                title: '添加新字典',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formAddDictionary.valid()) {

                                addDictionary();
                            }
                            else {
                                formAddDictionary.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgAddDictionary.hide();
                        }
                    }
                ]
            });
        }
    }

    function initAddDictionary() {
        formAddDictionary = $('#formAddDictionary').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { display: '值', name: 'Value', newline: true, type: 'text', validate: { required: true } }
                , { display: '分组', name: 'SubGroup', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });
    }
</script>

<script type="text/javascript">
    function updateDictionary() {
        $.post('/API/Dictionary/Update', formUpdateDictionary.getData(), function (data) {
            if (data.code == 0) {
                $.ligerDialog.success('修改成功', '提示');
                loadDictionaries();
                dlgUpdateDictionary.hide();
            } else {
                $.ligerDialog.error(data.msg, '错误');
            }
        }, 'json');
    }

    function showDlgUpdateDictionary(row) {
        if (dlgUpdateDictionary) {
            dlgUpdateDictionary.show();
        }
        else {
            dlgUpdateDictionary = $.ligerDialog.open({
                target: $('#dlgUpdateDictionary'),
                width: 440,
                height: 280,
                title: '修改字典',
                isResize: true,
                modal: true,
                buttons: [
                    {
                        text: '确定', onclick: function () {
                            if (formUpdateDictionary.valid()) {
                                updateDictionary();
                            }
                            else {
                                formUpdateDictionary.showInvalid();
                            }
                        }
                    },
                    {
                        text: '取消', onclick: function () {
                            dlgUpdateDictionary.hide();
                        }
                    }
                ]
            });
        }

        formUpdateDictionary.setData(row);
    }

    function initUpdateDictionary() {
        formUpdateDictionary = $('#formUpdateDictionary').ligerForm({
            inputWidth: 170, labelWidth: 90, space: 40,
            fields: [
                { name: 'Id', type: 'hidden' }
                , { display: '值', name: 'Value', newline: true, type: 'text', validate: { required: true } }
                , { display: '分组', name: 'SubGroup', newline: true, type: 'text', validate: { required: true } }
            ],
            validate: {}
        });
    }

    function deleteDictionary(row) {
        $.post('/API/Dictionary/Delete', { Id: row.Id, LoginName: row.LoginName },
            function (data) {
                if (data.code == 0) {
                    $.ligerDialog.success('删除成功', '提示');
                    loadDictionaries();
                } else {
                    $.ligerDialog.error(data.msg, '错误');
                }
            }, 'json');
    }
</script>

<script type="text/javascript">
    function itemclick(item) {
        switch (item.method) {
            case 'add':
                showDlgAddDictionary();
                break;

            case 'update':
                DoAction(gridDictionaries, function (row) {
                    showDlgUpdateDictionary(row);
                }, '请选择需要修改的字典');
                break;

            case 'delete':
                DoActionConfirm(gridDictionaries, deleteDictionary, '请选择需要修删除的字典', '是否要删除当前选择的字典？');
                break;
        }
    }

    var toolbarDictionaries = {
        items: [
            { method: 'add', text: '增加', click: itemclick, icon: 'add' },
            { line: true },
            { method: 'update', text: '修改', click: itemclick, icon: 'modify' },
            { line: true },
            { method: 'delete', text: '删除', click: itemclick, icon: 'delete' }
        ]
    };

    var columnDictionaries = [
        { display: '', name: 'Id', align: 'left', hide: true },
        { display: '值', name: 'Value', align: 'left', width: 150, minwidth: 120 },
        { display: '分组', name: 'SubGroup', align: 'left', width: 150, minwidth: 120 }
    ];

    function loadDictionaries() {
        var complete = false;
        $.post('/API/Dictionary/Query', {}, function (data) {
            complete = true;
            gridDictionaries.loadData(data);
            gridDictionaries.gridloading.hide(100);
        }, 'json');

        var n = setInterval(function () {
            clearInterval(n);
            if (!complete) {
                gridDictionaries.gridloading.show();
            }
        }, 100);
    }

    $(document).ready(function () {
        $.metadata.setType("attr", "validate");
        initAddDictionary();
        initUpdateDictionary();
        gridDictionaries = $('#gridDictionaries').ligerGrid({
            title: '字典信息',
            columns: columnDictionaries,
            toolbar: toolbarDictionaries,
            rownumbers: true,
            resizable: false,
            frozen: false,
            pageSize: 20,
            dataAction: 'local'
        });

        loadDictionaries();
    });
</script>